package com.sunlong.jiangwei.service.impl;

import com.google.errorprone.annotations.Var;
import com.sunlong.jiangwei.dao.*;
import com.sunlong.jiangwei.entity.*;
import com.sunlong.jiangwei.service.IJwOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 孙龙
 * @since 2021-02-19
 */
@Service
public class JwOrderServiceImpl implements IJwOrderService {

    @Autowired
    private JwOrderMapper orderMapper;

    @Autowired
    private JwCartMapper cartMapper;

    @Autowired
    private JwSpuMapper spuMapper;
    @Autowired
    private JwSkuMapper skuMapper;
    @Autowired
    private JwUserAddresslistMapper addresslistMapper;
    @Transactional
    @Override
    public Map<String, String> add(Map<String, Object> map) {
        HashMap<String, String> hashMap = new HashMap<>();
        Map<String,Object> address = (Map<String, Object>) map.get("address");
        ArrayList<Map<String,Object>> cartSelect = (ArrayList<Map<String, Object>>) map.get("cartSelect");
        Long userAddressId = ((Integer)address.get("userAddressId")).longValue();
        Long userId = ((Integer)map.get("userId")).longValue();
        for (Map<String, Object> good : cartSelect) {
            Long cartId = ((Integer)good.get("cartId")).longValue();
            JwCart jwCart = new JwCart(cartId);
            cartMapper.delete(jwCart);
            Integer count = (Integer) good.get("count");
            Map<String, Object> sku = (Map<String, Object>) good.get("sku");
            Map<String, Object> spu = (Map<String, Object>) good.get("spu");
            Long skuId = ((Integer)sku.get("skuId")).longValue();
            Long skuPrice = ((Integer)sku.get("skuPrice")).longValue();
            Long spuId = ((Integer)spu.get("spuId")).longValue();
            JwOrder jwOrder = new JwOrder();
            jwOrder.setOrderId(System.currentTimeMillis()+Math.round(Math.random()));
            jwOrder.setActualPay(count * skuPrice);
            jwOrder.setBuyNum(count);
            jwOrder.setStatus(1);
            jwOrder.setCreateTime(new Date());
            jwOrder.setSkuId(skuId);
            jwOrder.setSpuId(spuId);
            jwOrder.setUserId(userId);
            jwOrder.setUserAddressId(userAddressId);
            orderMapper.insert(jwOrder);
        }
        return hashMap;
    }

    @Override
    public List<JwOrder> list(Map<String, Object> map) {
        List<JwOrder> jwOrders = new ArrayList<>();
        if(null != map.get("userId")){
            Long userId = ((Integer) map.get("userId")).longValue();
            jwOrders = orderMapper.select(new JwOrder(userId));
        }else if (null != map.get("storeId")){
            Long storeId = ((Integer) map.get("storeId")).longValue();
            JwSpu spu = new JwSpu();
            spu.setStoreId(storeId);
            List<JwSpu> select = spuMapper.select(spu);
            ArrayList<JwOrder> orderArrayList = new ArrayList<>();
            for (JwSpu jwSpu : select) {
                JwOrder jwOrder = new JwOrder();
                jwOrder.setSpuId(jwSpu.getSpuId());
                List<JwOrder> select1 = orderMapper.select(jwOrder);
                orderArrayList.addAll(select1);
            }
            jwOrders = orderArrayList;
        }else {
            jwOrders = orderMapper.selectAll();
        }
        for (JwOrder jwOrder : jwOrders) {
            JwSpu spu = spuMapper.selectById(jwOrder.getSpuId());
            jwOrder.setSpu(spu);
            JwSku jwSku = skuMapper.selectOne(new JwSku(jwOrder.getSkuId()));
            jwOrder.setSku(jwSku);
            JwUserAddresslist addresslist = addresslistMapper.selectOne(new JwUserAddresslist(jwOrder.getUserAddressId()));
            jwOrder.setAddress(addresslist);
        }
        return jwOrders;
    }

    @Override
    public void status(Map<String, Object> map) {
        Long orderId = (Long) map.get("orderId");
        Integer status = (Integer)map.get("status");
        orderMapper.updateById(orderId,status);
    }
}
